<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------

namespace addons\Getui\controller;

require_once __DIR__ . '/' . '../sdk/GTClient.php';

/**
 * V2.1版本升级
 * 2021年10月2日
 * 新的个推控制器,服务端推送接口，支持三个接口推送
 * 该文件实现的是纯透传，一般是用于IOS在线不到消息的情况,请注意 此举会导致消息重复,前端要做好判断
 * 1.PushMessageToSingle接口：支持对单个用户进行推送
 * 2.PushMessageToList接口：支持对多个用户进行推送，建议为50个用户
 * 3.pushMessageToApp接口：对单个应用下的所有用户进行推送，可根据省份，标签，机型过滤推送
 * @package addons\Getui\controller
 * @author 晓风<215628355@qq.com>
 */
class Transmission
{
    // AppID
    protected $AppID;
    // AppKey
    protected $AppKey;
    // AppSecret
    protected $AppSecret;
    // masterSecret
    protected $MasterSecret;
    // 产品域名
    protected $domain = "https://restapi.getui.com";
    //开关
    protected $status;

    public function __construct()
    {      
        // 插件配置参数
        $config = addons_config('Getui');
        $this->AppID = $config['AppID'];
        $this->AppKey = $config['AppKey'];
        $this->AppSecret = $config['AppSecret'];
        $this->MasterSecret = $config['MasterSecret'];     
        $this->status = $config['status'];     
    }
    
    private function micro_time()
    {
        list($usec, $sec) = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }

    /**
     * 单推
     * 晓风<215628355@qq.com>]
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $client_id 客户端设备ID
     * @param string $custom    透传内容
     * @return pushMessageToSingle
     */
    public function pushMessageToSingle($title, $content, $client_id,$custom)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);        
        $push = $this->TransmissionTemplate($title,$content,$custom);
        $push->setCid($client_id);
        $res = $api->pushApi()->pushToSingleByCid($push);
        //执行推送
        return $res;     
    }


    /**
     * 多推
     * 晓风<215628355@qq.com>] 
    * @param string $title     消息标题
     * @param string $content   消息内容  
     * @param array   $client_id 客户端设备ID
     * @param string $custom    透传内容
     * @return pushMessageToSingle
     */
    function pushMessageToList($title, $content, $client_id , $custom)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);      
        $batch = new \GTPushBatchRequest();
        foreach($client_id as $id){
            $push = $this->TransmissionTemplate($title,$content,$custom);
            $push->setCid($id);
            $batch->addMsgList($push);
        }   
        $batch->setIsAsync(false);        
        $res = $api->pushApi()->pushBatchByCid($batch);
        //执行推送
        return $res;
    }
   /**
     * 群推
     * 晓风<215628355@qq.com>] 
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $custom    透传内容  
     * @return pushMessageToSingle
     */
    public function pushMessageToApp($title,$content,$custom)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);      
        $push = $this->TransmissionTemplate($title,$content,$custom);
        $push->setGroupName("test");   
        return  $api->pushApi()->pushAll($push);
    }  
    
    /**
     * 创建一个通用模板-发送纯透传消息
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $custom    透传内容   
     * @return IGtTransmissionTemplate
     */
    private function TransmissionTemplate($title,$content,$custom){
        $push = new \GTPushRequest();
        $push->setRequestId($this->micro_time());
        //设置setting
        $set = new \GTSettings();
        $set->setTtl(3600000);
        $push->setSettings($set);

        $message = new \GTPushMessage();        
      
        //纯透传消息，部分IOS设备前端展示时收不到消息，因为此时没有走ANPS通道，可以通过setTransmission 纯透传推送
        $message->setTransmission(json_encode([
            "title"=>$title,
            "content"=>$content, 
            "payload"=>$custom, 
        ],JSON_UNESCAPED_UNICODE));        
   
        $push->setPushMessage($message);          
        return $push;
    }
    
}